package com.mycompany.myproject.aop;

import org.aspectj.lang.ProceedingJoinPoint;
import org.aspectj.lang.annotation.*;
import org.springframework.stereotype.Component;

@Component
@Aspect
public class MyAop {

	/** * 定义一个公共的切点 */
	@Pointcut("execution(* com.mycompany.myproject.service.*.*(..))")
	public void performance() {

	}

	/** * 目标方法执行之前调用 */
	@Before("performance()")
	public void before1() {
		System.out.println("MyAop before1");
	}

	/** * 目标方法执行之前调用 */
	@Before("performance()")
	public void before2() {
		System.out.println("MyAop before2");
	}

	/** * 目标方法执行之前调用 */
	@Around("performance()")
	public Object around(ProceedingJoinPoint joinPoint) {

		Object obj = null;
		System.out.println("MyAop Aronud before...");
		try {
			obj = joinPoint.proceed();
		} catch (Throwable e) {
			e.printStackTrace();
		}
		System.out.println("MyAop Aronud after...");

		return obj;
	}

	/** * 目标方法执行完后调用 */
	@AfterReturning("performance()")
	public void afterReturning() {
		System.out.println("MyAop afterReturning");
	} /** * 目标方法发生异常时调用 */

	@AfterThrowing("performance()")
	public void afterThrowing() {
		System.out.println("MyAop afterThrowing");
	}
}
